package background

import (
	"github.com/astaxie/beego/logs"
	"github.com/gomodule/redigo/redis"
	"individual/controllers"
	"individual/models"
	"individual/utils"
	"math"
	"strings"
	"time"
)

type BlogController struct {
	controllers.BaseController
}

// AddBlog
// @Title AddBlog
// @Description 添加博客信息
// @Param blogName		formdata	string true		"博客标题"
// @Param blogContent	formdata	string true		"博客内容"
// @Param blogState		formdata	int true		"博客还是草稿，1-博客，0-草稿"
// @Param blogTop		formdata	int true		"是否置顶，1-置顶，0-不置顶"
// @Param photo			formdata	string true		"博客封面"
// @Param showWord		formdata	string true		"展示文字，默认是博客的前255个字符"
// @Param types			formdata	[]string true	"分类"
// @Param labels		formdata	[]string true	"标签"
// @Success 201 添加成功
// @Failure 403 添加失败
// @router /addBlog/ [post]
func (c *BlogController) AddBlog() {
	blogName := c.GetString("blogName")
	blogContent := c.GetString("blogContent")
	photo := c.GetString("photo")
	showWord := c.GetString("showWord")
	if showWord == "" {
		if len([]byte(utils.TrimHtml(utils.Markdown2Html(blogContent)))) < 400 {
			showWord = utils.TrimHtml(utils.Markdown2Html(blogContent))
		} else {
			showWord = string([]byte(utils.TrimHtml(utils.Markdown2Html(blogContent)))[0:400])
		}
	}
	blogState, err := c.GetInt("blogState")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "状态输入格式错误")
		c.ServeJSON()
		return
	}
	blogTop, err := c.GetInt("blogTop")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "置顶输入格式错误")
		c.ServeJSON()
		return
	}
	typeNames := c.GetStrings("types")
	labelNames := c.GetStrings("labels")
	var blog = models.Blog{BlogName: blogName,
		BlogState:     int8(blogState),
		BlogTop:       int8(blogTop),
		BlogShowWords: showWord,
		BlogContent:   blogContent,
		BlogPicture:   photo,
		UpdateAt:      time.Now(),
		View:          0,
	}
	var types []*models.Type
	var labels []*models.Label
	for _, typeName := range typeNames {
		if strings.Trim(typeName, " ") != "" {
			types = append(types, &models.Type{TypeName: typeName})
		}
	}
	for _, name := range labelNames {
		if strings.Trim(name, " ") != "" {
			labels = append(labels, &models.Label{LabelName: name})
		}
	}
	err = c.O.Begin()
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "启动添加博客事务失败")
		c.ServeJSON()
		return
	}
	// InsertMulti 如果插入的数据已经存在就不能再插入了，我们需要自己完成这一功能
	tlen := len(types)
	if tlen >= 1 {
		for i := 0; i < tlen; i++ {
			_, err = c.O.Insert(types[i])
			if err != nil {
				continue
			}
		}
	}
	llen := len(labels)
	if llen >= 1 {
		for i := 0; i < llen; i++ {
			_, err = c.O.Insert(labels[i])
			if err != nil {
				continue
			}
		}
	}
	//_, _ = c.O.InsertMulti(len(types), types)
	//_, _ = c.O.InsertMulti(len(labels), labels)
	id, err := c.O.Insert(&blog)
	if err != nil {
		_ = c.O.Rollback()
		c.Data["json"] = models.GetRevaErr(err, "启动添加博客事务失败")
		c.ServeJSON()
		return
	}
	types = []*models.Type{}
	labels = []*models.Label{}
	for _, typeName := range typeNames {
		var aType = &models.Type{TypeName: typeName}
		err = c.O.Read(aType, "TypeName")
		if err != nil {
			_ = c.O.Rollback()
			logs.Info("读取博客分类回滚")
			c.Data["json"] = models.GetRevaErr(err, "添加博客失败")
			c.ServeJSON()
			return
		}
		types = append(types, aType)
	}
	for _, name := range labelNames {
		var aLabel = &models.Label{LabelName: name}
		err = c.O.Read(aLabel, "LabelName")
		if err != nil {
			_ = c.O.Rollback()
			logs.Info("读取博客标签回滚")
			c.Data["json"] = models.GetRevaErr(err, "添加博客失败")
			c.ServeJSON()
			return
		}
		labels = append(labels, aLabel)
	}
	blog.Id = id
	// 添加多对多的表中的信息
	m1 := c.O.QueryM2M(&blog, "Types")
	for _, m2 := range types {
		_, err = m1.Add(m2)
		if err != nil {
			_ = c.O.Rollback()
			logs.Info("添加博客分类失败，err =", err)
			c.Data["json"] = models.GetRevaErr(err, "回滚--添加博客失败")
			c.ServeJSON()
			return
		}
	}

	m2 := c.O.QueryM2M(&blog, "Labels")
	for _, label := range labels {
		_, err = m2.Add(label)
		if err != nil {
			_ = c.O.Rollback()
			logs.Info("添加博客标签失败，err =", err)
			c.Data["json"] = models.GetRevaErr(err, "回滚--添加博客失败")
			c.ServeJSON()
			return
		}
	}

	err = c.O.Commit()
	if err != nil {
		_ = c.O.Rollback()
		c.Data["json"] = models.GetRevaErr(err, "提交添加博客事务失败")
		c.ServeJSON()
		return
	}
	blog.Types = types
	blog.Labels = labels
	c.Data["json"] = models.GetRevaOkAndDes(blog, "添加博客成功")
	c.ServeJSON()
}

// DeleteBlog
// @Title DeleteBlog
// @Description 删除博客
// @Param id path	int	true "博客id"
// @Success 201	Succeeded in deleting the blog by ID
// @Failure 403 Failed to delete blog by ID
// @router /deleteBlog/:id [delete]
func (c *BlogController) DeleteBlog() {
	id, err := c.GetInt64(":id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "id格式失败")
		c.ServeJSON()
		return
	}
	_, err = c.O.Delete(&models.Blog{Id: id})
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "后台操作数据库失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("删除成功")
	c.ServeJSON()
}

// UpdateBlog
// @Title UpdateBlog
// @Description 修改博客信息
// @Param id			formdata	int true		"博客id"
// @Param blogName		formdata	string true		"博客标题"
// @Param blogContent	formdata	string true		"博客内容"
// @Param blogState		formdata	int true		"博客还是草稿，1-博客，0-草稿"
// @Param blogTop		formdata	int true		"是否置顶，1-置顶，0-不置顶"
// @Param photo			formdata	string true		"博客封面"
// @Param showWord		formdata	string true		"展示文字，默认是博客的前255个字符"
// @Param types			formdata	[]string true	"分类"
// @Param labels		formdata	[]string true	"标签"
// @Success 201 添加成功
// @Failure 403 添加失败
// @router /updateBlog/ [put]
func (c *BlogController) UpdateBlog() {
	id, err := c.GetInt64("id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "id输入格式错误")
		c.ServeJSON()
		return
	}
	blogName := c.GetString("blogName")
	blogContent := c.GetString("blogContent")
	photo := c.GetString("photo")
	showWord := c.GetString("showWord")
	if showWord == "" {
		if len([]byte(utils.TrimHtml(utils.Markdown2Html(blogContent)))) < 400 {
			showWord = utils.TrimHtml(utils.Markdown2Html(blogContent))
		} else {
			showWord = string([]byte(utils.TrimHtml(utils.Markdown2Html(blogContent)))[0:400])
		}
	}
	blogState, err := c.GetInt("blogState")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "状态输入格式错误")
		c.ServeJSON()
		return
	}
	blogTop, err := c.GetInt("blogTop")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "置顶输入格式错误")
		c.ServeJSON()
		return
	}
	typeNames := c.GetStrings("types")
	labelNames := c.GetStrings("labels")
	var blog = models.Blog{
		Id:            id,
		BlogName:      blogName,
		BlogState:     int8(blogState),
		BlogTop:       int8(blogTop),
		BlogShowWords: showWord,
		BlogContent:   blogContent,
		BlogPicture:   photo,
		UpdateAt:      time.Now(),
	}
	var types []*models.Type
	var labels []*models.Label
	for _, typeName := range typeNames {
		if strings.Trim(typeName, " ") != "" {
			types = append(types, &models.Type{TypeName: typeName})
		}
	}
	for _, name := range labelNames {
		if strings.Trim(name, " ") != "" {
			labels = append(labels, &models.Label{LabelName: name})
		}
	}
	err = c.O.Begin()
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "启动更改博客事务失败")
		c.ServeJSON()
		return
	}
	//_, err = c.O.QueryTable("tab_blog_tab_types").Filter("tab_blog_id",id).Delete()
	//if err != nil {
	//	_ = c.O.Rollback()
	//	c.Data["json"] = models.GetRevaErr(err, "更改博客信息失败")
	//	c.ServeJSON()
	//	return
	//}
	//_, err = c.O.QueryTable("tab_blog_tab_labels").Filter("tab_blog_id",id).Delete()
	//if err != nil {
	//	_ = c.O.Rollback()
	//	c.Data["json"] = models.GetRevaErr(err, "更改博客信息失败")
	//	c.ServeJSON()
	//	return
	//}
	// InsertMulti 如果插入的数据已经存在就不能再插入了，我们需要自己完成这一功能
	tlen := len(types)
	if tlen >= 1 {
		for i := 0; i < tlen; i++ {
			_, err = c.O.Insert(types[i])
			if err != nil {
				continue
			}
		}
	}
	llen := len(labels)
	if llen >= 1 {
		for i := 0; i < llen; i++ {
			_, err = c.O.Insert(labels[i])
			if err != nil {
				continue
			}
		}
	}
	types = []*models.Type{}
	labels = []*models.Label{}
	for _, typeName := range typeNames {
		var aType = &models.Type{TypeName: typeName}
		err = c.O.Read(aType, "TypeName")
		if err != nil {
			_ = c.O.Rollback()
			c.Data["json"] = models.GetRevaErr(err, "更改博客信息失败")
			c.ServeJSON()
			return
		}
		types = append(types, aType)
	}
	for _, name := range labelNames {
		var aLabel = &models.Label{LabelName: name}
		err = c.O.Read(aLabel, "LabelName")
		if err != nil {
			_ = c.O.Rollback()
			c.Data["json"] = models.GetRevaErr(err, "更改博客信息失败")
			c.ServeJSON()
			return
		}
		labels = append(labels, aLabel)
	}
	// 修改多对多的表中的信息
	m1 := c.O.QueryM2M(&blog, "Types")
	_, _ = m1.Clear()
	for _, oneType := range types {
		_, err = m1.Add(oneType)
		if err != nil {
			_ = c.O.Rollback()
			c.Data["json"] = models.GetRevaErr(err, "更改博客信息失败")
			c.ServeJSON()
			return
		}
	}
	m2 := c.O.QueryM2M(&blog, "Labels")
	_, _ = m2.Clear()
	for _, label := range labels {
		_, err = m2.Add(label)
		if err != nil {
			_ = c.O.Rollback()
			c.Data["json"] = models.GetRevaErr(err, "更改博客信息失败")
			c.ServeJSON()
			return
		}
	}
	_, err = c.O.Update(&blog)
	if err != nil {
		_ = c.O.Rollback()
		c.Data["json"] = models.GetRevaErr(err, "更改博客信息失败")
		c.ServeJSON()
		return
	}
	err = c.O.Commit()
	if err != nil {
		_ = c.O.Rollback()
		c.Data["json"] = models.GetRevaErr(err, "提交更改博客事务失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("更改博客信息成功")
	c.ServeJSON()
}

// GetAllDrafts
// @Title	GetAllDrafts
// @Description 获取所有草稿信息
// @Success 201 获取所有草稿信息成功
// @Failure 403 获取所有草稿信息失败
// @router /getAllDrafts/ [get]
func (c *BlogController) GetAllDrafts() {
	var blogs []models.Blog
	_, err := c.O.QueryTable(new(models.Blog).TableName()).Filter("blog_state", 0).OrderBy("-blog_top", "-id").All(&blogs)
	for _, blog := range blogs {
		_, err = c.O.LoadRelated(&blog, "Types")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		_, err = c.O.LoadRelated(&blog, "Labels")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
	}
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取用户信息失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk(blogs)
	c.ServeJSON()
}

// GetViewsAndNumsInWeek
// @Title	GetViewsAndNumsInWeek
// @Description 获取一周中每天浏览量和评论数量
// @Success 201 获取一周中每天浏览量和评论数量成功
// @Failure 403 获取一周中每天浏览量和评论数量失败
// @router /getViewsAndNumsInWeek/ [get]
func (c *BlogController) GetViewsAndNumsInWeek() {
	views, err := new(models.Blog).GetEveryDayViewsInWeek()
	if err != nil && err != redis.ErrNil {
		c.Data["json"] = models.GetRevaErr(err, "获取每天的浏览量信息失败")
		c.ServeJSON()
		return
	}
	nums, err := new(models.LeaveWord).GetEveryDayNumsInWeek()
	if err != nil && err != redis.ErrNil {
		c.Data["json"] = models.GetRevaErr(err, "获取每天的留言量失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk(map[string]interface{}{
		"views": views,
		"nums":  nums,
	})
	c.ServeJSON()
}

// GetAllBlogViews
// @Title	GetAllBlogViews
// @Description Query all blogViews
// @Success 201 Query all blogViews successfully
// @Failure 403 Failed to query all blogViews
// @router /getAllBlogViews/ [get]
func (c *BlogController) GetAllBlogViews() {
	var bolg models.Blog
	views, err := bolg.GetAllViews()
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取博客总浏览量失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk(views)
	c.ServeJSON()
}

// GetBlogViews
// @Title	GetBlogViews
// @Description Query a blogView
// @Param	id 	path	int true	"Query a blogView by ID"
// @Success 201 Query a blogView successfully
// @Failure 403 Failed to query a blogView
// @router /getBlogViews/:id [get]
func (c *BlogController) GetBlogViews() {
	id, err := c.GetInt64(":id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取参数失败")
		c.ServeJSON()
		return
	}
	var blog = models.Blog{Id: id}
	views, err := blog.GetViews()
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取bolg浏览量失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk(views)
	c.ServeJSON()
}

// GetDraftByPage
// @Title GetDraftByPage
// @Description 分页获取草稿
// @Param pagesize	path	int true	"pagesize"
// @Param page	path	int true	"page"
// @Success 201 分页获取成功
// @Failure 403 分页获取失败
// @router /getDraftByPage/ [get]
func (c *BlogController) GetDraftByPage() {
	qs := c.O.QueryTable(new(models.Blog)).OrderBy("-blog_top", "-id").Filter("BlogState", 0)
	count, err := qs.Count()
	pageSetNum, err := c.GetInt("pagesize")
	if err != nil {
		pageSetNum = 1
	}
	//	总页数
	pageCount := math.Ceil(float64(count) / float64(pageSetNum))
	//	获取当前页码
	pageNum, err := c.GetInt("page")
	if err != nil {
		pageNum = 1
	}
	var blogs []models.Blog
	_, err = qs.Limit(pageSetNum, pageSetNum*(pageNum-1)).All(&blogs)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "错误")
		c.ServeJSON()
		return
	}
	for _, blog := range blogs {
		_, err = c.O.LoadRelated(&blog, "Types")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		_, err = c.O.LoadRelated(&blog, "Labels")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
	}
	c.Data["json"] = models.GetRevaOk(map[string]interface{}{
		"num":       count,
		"pageNum":   pageNum,
		"pageCount": pageCount,
		"blogs":     blogs,
	})
	c.ServeJSON()
}

// GetDraftsByBlogNamePage
// @Title GetDraftsByBlogNamePage
// @Description 根据博客名分页模糊查询草稿
// @Param blogName	path	string true	"模糊查询"
// @Param pagesize	path	int true	"pagesize"
// @Param page	path	int true	"page"
// @Success 201 根据博客名分页模糊查询草稿成功
// @Failure 403 根据博客名分页模糊查询草稿失败
// @router /getDraftsByBlogNamePage/ [get]
func (c *BlogController) GetDraftsByBlogNamePage() {
	blogname := c.GetString("blogName")
	qs := c.O.QueryTable(new(models.Blog).TableName()).Filter("blog_name__contains", blogname).OrderBy("-blog_top", "-id").Filter("BlogState", 0)
	count, err := qs.Count()
	pageSetNum, err := c.GetInt("pagesize")
	if err != nil {
		pageSetNum = 1
	}
	//	总页数
	pageCount := math.Ceil(float64(count) / float64(pageSetNum))
	//	获取当前页码
	pageNum, err := c.GetInt("page")
	if err != nil {
		pageNum = 1
	}
	var blogs []models.Blog
	_, err = qs.Limit(pageSetNum, pageSetNum*(pageNum-1)).All(&blogs)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "错误")
		c.ServeJSON()
		return
	}
	for i := 0; i < len(blogs); i++ {
		_, err = c.O.LoadRelated(&blogs[i], "Types")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		_, err = c.O.LoadRelated(&blogs[i], "Labels")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
	}
	c.Data["json"] = models.GetRevaOk(map[string]interface{}{
		"num":       count,
		"pageNum":   pageNum,
		"pageCount": pageCount,
		"blogs":     blogs,
	})
	c.ServeJSON()
}

// GetBlogByPage
// @Title GetBlogByPage
// @Description 分页获取博客
// @Param pagesize	path	int true	"pagesize"
// @Param page	path	int true	"page"
// @Success 201 分页获取成功
// @Failure 403 分页获取失败
// @router /getBlogByPage/ [get]
func (c *BlogController) GetBlogByPage() {
	qs := c.O.QueryTable(new(models.Blog).TableName()).OrderBy("-blog_top", "-id").Filter("BlogState", 1)
	count, err := qs.Count()
	pageSetNum, err := c.GetInt("pagesize")
	if err != nil {
		pageSetNum = 1
	}
	//	总页数
	pageCount := math.Ceil(float64(count) / float64(pageSetNum))
	//	获取当前页码
	pageNum, err := c.GetInt("page")
	if err != nil {
		pageNum = 1
	}
	var blogs []models.Blog
	_, err = qs.Limit(pageSetNum, pageSetNum*(pageNum-1)).All(&blogs)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "错误")
		c.ServeJSON()
		return
	}
	for i := 0; i < len(blogs); i++ {
		_, err = c.O.LoadRelated(&blogs[i], "Types")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		_, err = c.O.LoadRelated(&blogs[i], "Labels")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		blogs[i].View, _ = blogs[i].GetViews()
	}
	c.Data["json"] = models.GetRevaOk(map[string]interface{}{
		"num":       count,
		"pageNum":   pageNum,
		"pageCount": pageCount,
		"blogs":     blogs,
	})
	c.ServeJSON()
}

// AddType
// @Title AddType
// @Description 单独添加分类标签
// @Param typename formdata string true "分类名"
// @Success 201 添加成功
// @Failure 403 添加失败
// @router /addType/ [post]
func (c *BlogController) AddType() {
	var aType models.Type
	aType.TypeName = c.GetString("typename")
	_, err := c.O.Insert(&aType)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "添加分类失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("添加分类成功")
	c.ServeJSON()
}

// AddLabel
// @Title AddLabel
// @Description 单独添加标签标签
// @Param labelname formdata string true "标签名"
// @Success 201 添加成功
// @Failure 403 添加失败
// @router /addLabel/ [post]
func (c *BlogController) AddLabel() {
	var aLabel models.Label
	aLabel.LabelName = c.GetString("labelname")
	_, err := c.O.Insert(&aLabel)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "添加标签失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("添加标签成功")
	c.ServeJSON()
}

// GetBlogsByBlogNameAndPage
// @Title GetBlogsByBlogNameAndPage
// @Description 模糊查询博客
// @Param blogName	path	string true	"模糊查询"
// @Param pagesize	path	int true	"每页的数量"
// @Param page	path	int true	"第几页"
// @Success 201 模糊查询博客成功
// @Failure 403 模糊查询博客失败
// @router /getBlogsByBlogNameAndPage/ [get]
func (c *BlogController) GetBlogsByBlogNameAndPage() {
	blogname := c.GetString("blogName")
	qs := c.O.QueryTable(new(models.Blog).TableName()).OrderBy("-blog_top", "-id").Filter("BlogState", 1).Filter("blog_name__contains", blogname)
	count, err := qs.Count()
	pageSetNum, err := c.GetInt("pagesize")
	if err != nil {
		pageSetNum = 1
	}
	//	总页数
	pageCount := math.Ceil(float64(count) / float64(pageSetNum))
	//	获取当前页码
	pageNum, err := c.GetInt("page")
	if err != nil {
		pageNum = 1
	}
	var blogs []models.Blog
	_, err = qs.Limit(pageSetNum, pageSetNum*(pageNum-1)).All(&blogs)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "错误")
		c.ServeJSON()
		return
	}
	for i := 0; i < len(blogs); i++ {
		_, err = c.O.LoadRelated(&blogs[i], "Types")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		_, err = c.O.LoadRelated(&blogs[i], "Labels")
		if err != nil {
			c.Data["json"] = models.GetRevaErr(err, "获取博客信息失败")
			c.ServeJSON()
			return
		}
		blogs[i].View, _ = blogs[i].GetViews()
	}
	c.Data["json"] = models.GetRevaOk(map[string]interface{}{
		"num":       count,
		"pageNum":   pageNum,
		"pageCount": pageCount,
		"blogs":     blogs,
	})
	c.ServeJSON()
}

// UpdateLabel
// @Title UpdateLabel
// @Description 修改标签
// @Param id formdata string true "标签id"
// @Param labelname formdata string true "标签名"
// @Success 201 修改标签成功
// @Failure 403 修改标签失败
// @router /updateLabel/ [put]
func (c *BlogController) UpdateLabel() {
	labelName := c.GetString("labelname")
	id, err := c.GetInt64("id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "传入参数的格式错误")
		c.ServeJSON()
		return
	}
	_, err = c.O.Update(&models.Label{Id: id, LabelName: labelName}, "LabelName")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "修改标签失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("修改标签成功")
	c.ServeJSON()
}

// UpdateType
// @Title UpdateType
// @Description 修改分类
// @Param id formdata string true "分类id"
// @Param typename formdata string true "分类名"
// @Success 201 修改分类成功
// @Failure 403 修改分类失败
// @router /updateType/ [put]
func (c *BlogController) UpdateType() {
	typename := c.GetString("typename")
	id, err := c.GetInt64("id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "传入参数的格式错误")
		c.ServeJSON()
		return
	}
	_, err = c.O.Update(&models.Type{Id: id, TypeName: typename}, "TypeName")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "修改分类失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("修改分类成功")
	c.ServeJSON()
}

// DeleteType
// @Title DeleteType
// @Description 删除分类
// @Param id path int true "分类id"
// @Success 201 删除成功
// @Failure 403 删除失败
// @router /delType/:id [delete]
func (c *BlogController) DeleteType() {
	id, err := c.GetInt64(":id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "传入参数格式错误")
		c.ServeJSON()
		return
	}
	_ = c.O.Begin()
	_, err = c.O.Delete(&models.Type{Id: id})
	if err != nil {
		_ = c.O.Rollback()
		c.Data["json"] = models.GetRevaErr(err, "删除分类失败")
		c.ServeJSON()
		return
	}
	_, err = c.O.QueryTable("tab_blog_tab_types").Filter("tab_type_id", id).Delete()
	if err != nil {
		_ = c.O.Rollback()
		c.Data["json"] = models.GetRevaErr(err, "删除分类失败")
		c.ServeJSON()
		return
	}
	_ = c.O.Commit()
	c.Data["json"] = models.GetRevaOk("删除分类成功")
	c.ServeJSON()
}

// DeleteLabel
// @Title DeleteLabel
// @Description 删除标签
// @Param id path int true "标签名"
// @Success 201 删除成功
// @Failure 403 删除失败
// @router /delLabel/:id [delete]
func (c *BlogController) DeleteLabel() {
	id, err := c.GetInt64(":id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "传入参数格式错误")
		c.ServeJSON()
		return
	}
	_ = c.O.Begin()
	_, err = c.O.Delete(&models.Label{Id: id})
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "删除标签失败")
		c.ServeJSON()
		return
	}
	_, err = c.O.QueryTable("tab_blog_tab_labels").Filter("tab_label_id", id).Delete()
	if err != nil {
		_ = c.O.Rollback()
		c.Data["json"] = models.GetRevaErr(err, "删除分类失败")
		c.ServeJSON()
		return
	}
	_ = c.O.Commit()
	c.Data["json"] = models.GetRevaOk("删除标签成功")
	c.ServeJSON()
}

// GetTotalData
// @Title GetTotalData
// @Description 获取留言数、博客数、草稿数、总浏览量
// @Success 201 获取总数据成功
// @Failure 403 获取总数据失败
// @router /getTotalData/ [get]
func (c *BlogController) GetTotalData() {
	var b = models.Blog{}
	allViews, err := b.GetAllViews()
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取总浏览量失败")
		c.ServeJSON()
		return
	}
	blogNum, err := c.O.QueryTable(b.TableName()).Filter("blog_state", 1).Count()
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取博客数量失败")
		c.ServeJSON()
		return
	}
	draftNum, err := c.O.QueryTable(b.TableName()).Filter("blog_state", 0).Count()
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取草稿数量失败")
		c.ServeJSON()
		return
	}
	count1, err := c.O.QueryTable(new(models.LeaveWord).TableName()).Count()
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取留言数量失败")
		c.ServeJSON()
		return
	}
	count2, err := c.O.QueryTable(new(models.LeaveWordTwo).TableName()).Count()
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "获取留言数量失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk(map[string]interface{}{
		"leaveWordNum": count1 + count2,
		"blogNum":      blogNum,
		"draftNum":     draftNum,
		"AllViews":     allViews,
	})
	c.ServeJSON()
}
